package controllers;

import play.*;
import play.modules.paginate.ValuePaginator;
import play.data.validation.Required;
import play.mvc.*;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.jdt.internal.compiler.ast.ForeachStatement;
import org.hibernate.dialect.function.TemplateRenderer;

import com.google.gson.JsonElement;

import common.FBSecure;
import common.FBUtil;
import common.TVRageRequest;
import common.iMDBRequest;

import models.*;

@With(FBSecure.class)
public class Series extends FBController {
	
	public static void series(String param, String type) {
		setFbIdandName();
		if(param != null)
			param = param.toLowerCase();
		
		List<Serie> serieLists = Serie.getAllSpecificCharSeries(param, type);
		
		
		if (!serieLists.equals(null)){
			for (Serie serie : serieLists) {
				serie.calcAvgRate();
			}
		}
		getUser().save();
				
		ValuePaginator paginator = new ValuePaginator(serieLists);
		paginator.setPageSize(10);
		
		render(serieLists, param, type, paginator);
	}
	
	
    public static void series_detail(long id) {
    	
    	setFbIdandName();
    	
    	User user = getUser();
    	Serie series = Serie.findById(id);
    	
    	if (series != null) {
    		series.calcAvgRate();
    	}
    	
    	ValuePaginator paginator = new ValuePaginator(series.getCommentList());
		paginator.setPageSize(5);

    	Rate rate = null;
    	
    	for (Rate r : user.getRates()) {
    		if (r.getSerie().getId() == series.getId()) {
    			rate = r;
    			break;
    		}
    	}
    	
        render(series, user, rate, paginator);

    }

    
    public static void follow(long id) {

    	setFbIdandName();
    	
    	User user = getUser();
    	Serie series = Serie.findById(id);
    	
    	series.addFollower(user);
    	user.addSeries(series);
    	user.save();
    	series.save();
    	
    	series.calcAvgRate();
    	
    	Rate rate = null;
    	
    	for (Rate r : user.getRates()) {
    		if (r.getSerie().getId() == series.getId()) {
    			rate = r;
    			break;
    		}
    	}
    	

        ValuePaginator paginator = new ValuePaginator(series.getCommentList());
		paginator.setPageSize(5);
		
    	renderTemplate("Series/series_detail.html", series, user, rate, paginator);
    	
    }
    
    public static void directfollow(long ref) {
    	
    	setFbIdandName();
    	
    	User user = getUser();
    	Serie series = Serie.findById(ref);

    	series.addFollower(user);
    	user.addSeries(series);
    	user.save();
    	series.save();
    	
    	series.calcAvgRate();
    	
    }
    
    public static float giveRate(int rating, long sid){
    	
    	User user = getUser();
    	Serie series = Serie.findById(sid);

    	for (Rate r : series.getRateList()) {
    		
    		if (r.getUser().getFacebookID().equals(user.getFacebookID())) {
    			series.removeRate(r);
    			r.delete();
    			break;
    		}
    	}
    	
    	Rate newRate = new Rate(user, series, rating);
    	newRate.save();
    	
    	series.addRate(newRate);
    	
    	series.calcAvgRate();
    	series.save();
    	
    	return series.getAvgRate();
    	
    }
   	
  
    
    
    public static void unfollow(long id) {

    	setFbIdandName();
    	
    	User user = getUser();
    	Serie series = Serie.findById(id);
    	
    	series.removeFollower(user);
    	user.removeSeries(series);
    	user.save();
    	series.save();
    	
    	series.calcAvgRate();
    	
    	Rate rate = null;
    	
    	for (Rate r : user.getRates()) {
    		if (r.getSerie().getId() == series.getId()) {
    			rate = r;
    			break;
    		}
    	}
    	
    	ValuePaginator paginator = new ValuePaginator(series.getCommentList());
		paginator.setPageSize(5);
		
    	renderTemplate("Series/series_detail.html", series, user, rate, paginator);
    }
    
    
  
    
    
    public static void postComment(long id, @Required String comment) {
    	
    	User user = getUser();
        Serie series = Serie.findById(id);

		ValuePaginator paginator = new ValuePaginator(series.getCommentList());
		paginator.setPageSize(5);
		
        if (validation.hasErrors()) {
        	renderTemplate("Series/series_detail.html", series, user, paginator);
		}
        
        user.addComment(series, comment);
        
        user.save();

		series_detail(id);
       
    }
    
    
    
    public static void deleteComment(long id, long cid) {
    	
    	User user = getUser();
        CommentSerie comment = CommentSerie.findById(cid);
        
        if (user.getComments().contains(comment)){
	        user.removeComment(comment);
	        comment.delete();
	        user.save();
        }
        
		series_detail(id);
    }

    
    
    
    public static void search(String param, String type) {
		
		setFbIdandName();

		List<Serie> serieLists = Serie.searchSeries(param, type);
		
		if (!serieLists.equals(null)){
			for (Serie serie : serieLists) {
				serie.calcAvgRate();
			}
		}
		
		ValuePaginator paginator = new ValuePaginator(serieLists);
		paginator.setPageSize(10);
				
		render(serieLists, param, paginator);
	}
    
    public static void topicOrder(long id, String type) {
		
		setFbIdandName();
		
		User user = getUser();
    	Serie series = Serie.findById(id);

		List<Topic> topicList = Serie.getAllTopicOrder(id, type);
		
		ValuePaginator paginator = new ValuePaginator(topicList);
		paginator.setPageSize(5);
		
		renderTemplate("Series/series_topics.html", user, series, topicList, paginator);
	}

    
    
    public static void series_topics(long id) {
    	
    	setFbIdandName();
    	
    	User user = getUser();
    	Serie series = Serie.findById(id);
    	

    	
    	if (series != null) {
    		series.calcAvgRate();
    	}
    	
    	
    	List<Topic> topicList = series.getTopicList();
    	
        ValuePaginator paginator = new ValuePaginator(topicList);
		paginator.setPageSize(5);
    	
        render(series, user, topicList, paginator);

    }    
    
    
    public static void series_episodes(long id) {
    	
    	setFbIdandName();
    	
    	User user = getUser();
    	Serie series = Serie.findById(id);
    	

    	
    	if (series != null) {
    		series.calcAvgRate();
    	}
    	
        render(series, user);

    }
    
    
    public static void new_topic(long id) {
    	
    	setFbIdandName();
    	
    	User user = getUser();
    	Serie series = Serie.findById(id);
    	    	
        render(series, user);

    }
    
    
    
    public static void add_new_topic(long id, @Required String topicName, @Required String content) {
    	
    	User user = getUser();
        Serie series = Serie.findById(id);
        Date creationDate = new Date();        
        
        if (validation.hasErrors()) {
        	renderTemplate("Series/new_topic.html", series);
        	}   
        
        user.addTopic(series, topicName, content, creationDate);        
        user.save();
        
		series_topics(id);
    }
    
    
    public static void delete_topic(long id, long tid) {
    	
    	User user = getUser();
        Topic topic = Topic.findById(tid);
        
        if (user.getTopicList().contains(topic)){
	        user.removeTopic(topic);
	        topic.delete();
	        user.save();
        }
        
        series_topics(id);
    }
    
    
    public static void topic_detail(long id, long tid) {
    	
    	setFbIdandName();
    	
    	User user = getUser();
        Serie series = Serie.findById(id);
        Topic topic = Topic.findById(tid);
        
        ValuePaginator paginator = new ValuePaginator(topic.getCommentTopicList());
		paginator.setPageSize(5);
        
    	render(series, user, topic, paginator);
    }
    
    
    public static void postCommentToTopic(long id, long tid, @Required String comment) {
    	
    	setFbIdandName();
    	
    	User user = getUser();
    	Topic topic = Topic.findById(tid);
        Serie series = Serie.findById(id);

        ValuePaginator paginator = new ValuePaginator(topic.getCommentTopicList());
		paginator.setPageSize(5);
		
		if (validation.hasErrors()) {
			renderTemplate("Series/topic_detail.html", series, topic, user, paginator);
		}
        
        user.addCommentToTopic(topic, comment);
        topic.setCommentNum(topic.getCommentTopicList().size());
        topic.save();
        user.save();
		
        topic_detail(id, tid);
    }
    
    
    
    public static void deleteCommentFromTopic(long id, long tid, long cid) {
    	    	
    	User user = getUser();
    	Topic topic = Topic.findById(tid);
        CommentTopic comment = CommentTopic.findById(cid);        
        
        if (user.getComments().contains(comment)){
	        user.removeComment(comment);
	        comment.delete();
	        topic.setCommentNum(topic.getCommentTopicList().size());
	        user.save();
        }

        topic_detail(id, tid);
    }
    
    

    
    public static void add_video(long id) {
    	
    	setFbIdandName();
    	
    	User user = getUser();
        Serie series = Serie.findById(id);
    	
        
        render(series, user);
        
    }
    
    public static void add_new_video(long id, @Required String name, @Required String address) {
    	
    	setFbIdandName();
    	
    	User user = getUser();
        Serie series = Serie.findById(id);
        
        URL url;
        String youtubeid;
        
        if (validation.hasErrors()) {
        	renderTemplate("Series/add_video.html", series, user);
		}
        
		try {
			url = new URL(address);
			
			youtubeid = url.getQuery();
			
			youtubeid = youtubeid.substring(2);
			
	        Video newVideo = new Video(name, youtubeid, "youtube", series, user);
	        newVideo.save();
	        user.addVideo(newVideo);

	        
	        
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
        
		List<Video> videos = Video.getAllVideos(id);

        ValuePaginator paginator = new ValuePaginator(videos);
		paginator.setPageSize(5);
		
        renderTemplate("Videos/videos.html", series, user, videos, paginator);
		    	
    }
}
